define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'partner/file/sign/index' + location.search,
                    add_url: 'partner/file/sign/add',
                    edit_url: 'partner/file/sign/edit',
                    del_url: 'partner/file/sign/del',
                    multi_url: 'partner/file/sign/multi',
                    import_url: 'partner/file/sign/import',
                    table: 'partner_file_sign',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'partner.partner_account', title: '合伙人帐号', operate: 'LIKE'},
                        {field: 'file_one_signature_image', title: __('File_one_signature_image'), operate: false, events: Table.api.events.image, formatter: Controller.api.formatter.image},
                        {field: 'file_two_signature_image', title: __('File_two_signature_image'), operate: false, events: Table.api.events.image, formatter: Controller.api.formatter.image},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        readonly: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'partner/file/sign/index' + location.search,
                    add_url: 'partner/file/sign/add',
                    edit_url: 'partner/file/sign/edit',
                    del_url: 'partner/file/sign/del',
                    multi_url: 'partner/file/sign/multi',
                    import_url: 'partner/file/sign/import',
                    table: 'partner_file_sign',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'partner.partner_account', title: '合伙人帐号', operate: 'LIKE'},
                        {field: 'file_one_signature_image', title: __('File_one_signature_image'), operate: false, events: Table.api.events.image, formatter: Controller.api.formatter.image},
                        {field: 'file_two_signature_image', title: __('File_two_signature_image'), operate: false, events: Table.api.events.image, formatter: Controller.api.formatter.image},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            $('div.fixed-table-toolbar').hide();

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                image: function (value, row, index) {
                    value = value == null || value.length === 0 ? '' : value.toString();
                    if (!value) {
                        return '-';
                    }
                    var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
                    var url = Fast.api.cdnurl(value, true);
                    url = url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle;
                    return '<a href="'+url+'" target="_blank"><img src="' + url + '" style="width: 30px; height: 30px; margin: 0 auto; display: inline; float: none;"/></a>';
                },
            }
        }
    };
    return Controller;
});
